<?php
/**
 * 管理端登陆入口
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class PublicAction extends AppAction
{
    /**
     * 管理端登陆页面
     */
    public function login()
    {
        // 检查是否已经登陆了
        if(session('?'.C('ADMIN_SESSION_KEY')))
        {
            $this->redirect('Admin/Dashboard/index');
        }
        if(IS_POST)
        {
            $show_info = array('url'=>'Admin/Public/login');
            $Member = D('Member');
            $Member->setValidate('login');
            if($Member->create($this->data))
            {
                // 验证条件
                $where = array
                (
                    'account'      => $this->data['username'],
                    'password'     => password_encode($this->data['password']),
                    'account_type' => 2
                );
                $fields = array('id','account','password','nickname','role_id','active_status');
                $result = $Member->field($fields)->where($where)->find();
                if(empty($result))
                {
                    $show_info['message'] = '登陆信息错误,请确认后再次登陆!';
                    $this->error($show_info);
                }
                if($result['active_status'] == 2)
                {
                    $show_info['message'] = '该账号已被禁止登陆!';
                    $this->error($show_info);
                }
                if($result['active_status'] == 1)
                {
                    $show_info['message'] = '该登陆账号未激活!!';
                    $this->error($show_info);
                }
                // 权限设置
                if($result['role_id'] != 1 && !is_null($result['role_id']))
                {
                    // 获取对应的角色权限
                    $result['auth'] = D('RoleNode')->getRoleNodeIds($result['role_id']);
                }
                elseif($result['role_id'] == 1)
                {
                    $result['auth'] = '*';
                }
                // 记录登陆日志
                $LoginLog = D('LoginLog');
                $log_data = array
                (
                    'member_id' => $result['id'],
                    'account'   => $result['account'],
                    'ip'        => get_client_ip()
                );
                $LoginLog->add($log_data);
                // 保存登陆信息到Session
                session(C('ADMIN_SESSION_KEY'),$result);
                $show_info = array
                (
                    'url'       => 'Admin/Dashboard/index',
                    'message'   => '登陆成功!正在跳转至管理页面...',
                    'wait_time' => 1
                );
                $this->success($show_info);
            }
            else
            {
                $show_info['message'] = $Member->getError();
                $this->error($show_info);
            }
        }
        $this->display();
    }
    
    public function logout()
    {
        // 清除登陆保存的Session
        session(C('ADMIN_SESSION_KEY'),null);
        $show_info = array
        (
            'url'       => 'Admin/Public/login',
            'message'   => '注销成功!',
            'wait_time' => 1
        );
        $this->success($show_info);
    }
}